#include "stdafx.h"


namespace Melting
{   
	//-------------------------------------------------------------------------------------
	// CTOR
	//-------------------------------------------------------------------------------------
	Timer::Timer( void )
	{
		// Get the frequency of the timer
		LARGE_INTEGER qwTicksPerSec;

		QueryPerformanceFrequency( &qwTicksPerSec );
		mfSecsPerTick = FLOAT(1.0f) / FLOAT(qwTicksPerSec.QuadPart);

		// Save the start time
		QueryPerformanceCounter( &mqwTime );	
	    
		// Zero out the elapsed and total time
		mqwAppTime.QuadPart		= 0;

		mfAbsoluteTime			= FLOAT(0.0f); 
		mfAppTime		= FLOAT(0.0f); 
		mfElapsedTime	= FLOAT(0.0f);

		mulNbFrames		= 0;
		mfLastFPSTime	= 0;
		mfFPS			= FLOAT( 0 );
	} 

	//-------------------------------------------------------------------------------------
	// Update the Timer (call it each frame!)
	//-------------------------------------------------------------------------------------
	void Timer::Update( void )
	{
		LARGE_INTEGER	qwNewTime;
		LARGE_INTEGER	qwDeltaTime;
	    
		QueryPerformanceCounter( &qwNewTime );

		mfAbsoluteTime			= mfSecsPerTick * ((FLOAT)(qwNewTime.QuadPart)); 

		qwDeltaTime.QuadPart	= qwNewTime.QuadPart - mqwTime.QuadPart;

		mqwAppTime.QuadPart		+= qwDeltaTime.QuadPart;    
		mqwTime.QuadPart		= qwNewTime.QuadPart;
	    
		mfElapsedTime   = mfSecsPerTick * ((FLOAT)(qwDeltaTime.QuadPart));
		mfAppTime       = mfSecsPerTick * ((FLOAT)(mqwAppTime.QuadPart));   

		mulNbFrames ++; 

		// Only re-compute the FPS (frames per second) once per second
		if( mfAbsoluteTime - mfLastFPSTime > FLOAT(1.0f) )
		{
			mfFPS = mulNbFrames / ( mfAbsoluteTime - mfLastFPSTime );
			mfLastFPSTime = mfAbsoluteTime;
			mulNbFrames = 0L;  
		}
	}
}

